﻿using HarmonyLib;
using RimWorld;
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Verse;
using Verse.AI;

namespace RimZombieGirl
{
    [HarmonyPatch(typeof(BookUtility))]
    [HarmonyPatch("IsValidBook")]
    public static class HarmonyPatch_ForBook
    {
        [HarmonyPostfix]
        public static void RemoveBooks(ref bool __result, Thing thing)
        {
            bool flag = thing is Book_AbilityLimitSkill;
            if (flag)
            {
                __result = false;
            }
        }
    }
    [HarmonyPatch(typeof(JobDriver_Reading))]
    [HarmonyPatch("ReadBook")]
    public static class HeAiFist_JobDriver_Reading_ReadBook_Patch
    {
        [HarmonyPostfix]
        public static void CommunicateFinishedReading(ref Toil __result, JobDriver_Reading __instance)
        {
            __result.AddFinishAction(delegate
            {
                BookDefModExtension modExtension = __instance.Book.def.GetModExtension<BookDefModExtension>();
                bool flag = modExtension != null && !modExtension.readResults.NullOrEmpty<BookReadResults>();
                if (flag)
                {
                    foreach (BookReadResults bookReadResults in modExtension.readResults)
                    {
                        bookReadResults.doerClass.GetMethod("Notify_BookRead").Invoke(null, new object[]
                        {
                            __instance.pawn,
                            __instance.Book
                        });
                    }
                }
            });
        }
    }

    public class BookDefModExtension : DefModExtension
    {
        public List<BookReadResults> readResults;
    }
    public class BookReadResults
    {
        public Type doerClass;
    }
}
